package com.zx.map.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import com.zx.map.dao.AgentInfoDao;
import com.zx.map.dao.MerchantInfoDao;
import com.zx.map.dao.SettleCardInfoDao;
import com.zx.map.dao.model.AgentInfo;
import com.zx.map.dao.model.AgentInfoExample;
import com.zx.map.dao.model.MerchantInfo;
import com.zx.map.dao.model.MerchantInfoExample;
import com.zx.map.dao.model.SettleCardInfo;
import com.zx.map.dao.model.SettleCardInfoExample;
import com.zx.map.service.SettleCardInfoService;
import com.zx.map.service.TodoTaskService;
import com.zx.map.service.domain.SettleCardInfoDomain;
import com.zx.map.service.model.SettleCardInfoModel;
import com.zx.map.service.model.TodoTaskModel;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("settleCardInfoService")
public class SettleCardInfoServiceImpl implements SettleCardInfoService {

	@Autowired
	private SettleCardInfoDao dao;
	@Autowired
	private AgentInfoDao agentInfoDao;
	@Autowired
	private MerchantInfoDao merchantInfoDao;
	@Autowired
	private TodoTaskService todoTaskService;
	/**
	 * 得到所有数据列表
	 */
	public List getAllList() throws DataAccessException {

		List<SettleCardInfo> list = dao.getListByExample(null);
		List<SettleCardInfoModel> modelList = new ArrayList<SettleCardInfoModel>();
		for (SettleCardInfo o : list) {
			SettleCardInfoModel temp = new SettleCardInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;

	}

	/**
	 * 根据单Key查找Model
	 */
	public SettleCardInfoModel getModelByKey(Integer key) throws DataAccessException {
		SettleCardInfo o = dao.selectByPrimaryKey(key);
		SettleCardInfoModel model = new SettleCardInfoModel();
		BeanUtils.copyProperties(o, model);
		return model;
	}

	/**
	 * 根据Model查找列表
	 */
	public List getListByModel(SettleCardInfoModel model) throws DataAccessException {
		SettleCardInfoExample example = new SettleCardInfoExample();
		com.zx.map.dao.model.SettleCardInfoExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(model.getBankAccount())) {
			criteria.andBankAccountEqualTo(model.getBankAccount());
		}
		//增加按商户号查询
		if (StringUtils.isNotBlank(model.getSubjectId())) {
			criteria.andSubjectIdEqualTo(model.getSubjectId());
		}
		List<SettleCardInfo> list = dao.getListByExample(example);
		List<SettleCardInfoModel> modelList = new ArrayList<SettleCardInfoModel>();
		for (SettleCardInfo o : list) {
			SettleCardInfoModel temp = new SettleCardInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}

	/**
	 * 新增
	 */
	public Integer add(SettleCardInfoModel model) throws DataAccessException {
		SettleCardInfo temp = new SettleCardInfo();
		BeanUtils.copyProperties(model, temp);
		Integer key = dao.add(temp);
		return key;
	}

	/**
	 * 修改
	 */
	public Integer update(SettleCardInfoModel model) throws DataAccessException {
		SettleCardInfo temp = new SettleCardInfo();
		BeanUtils.copyProperties(model, temp);
		dao.update(temp);
		return model.getSettleId();
	}

	/**
	 * 删除
	 */
	public void delete(SettleCardInfoModel model) throws DataAccessException {
		SettleCardInfo temp = new SettleCardInfo();
		BeanUtils.copyProperties(model, temp);
		dao.delete(temp);
	}

	/**
	 * 分页查询
	 * 
	 * @param pageSize
	 * @param pageNum
	 * @param menuModel
	 * @return
	 * @throws DataAccessException
	 */
	public Map getPageByDomain(int pageSize, int pageNum, SettleCardInfoDomain domain)
			throws DataAccessException {

		Map<String, Object> map = new HashMap<String, Object>();
		SettleCardInfoExample example = new SettleCardInfoExample();
		com.zx.map.dao.model.SettleCardInfoExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(domain.getBankAccount())) {
			criteria.andBankAccountEqualTo(domain.getBankAccount());
		}
		//排序add by wenjw 2015.6.2
		example.setOrderByClause("create_Date desc");
		int pageBegin = (pageNum - 1) * pageSize;
		List<SettleCardInfo> list = dao.getPageByExample(pageSize, pageBegin, example);
		List<SettleCardInfoModel> menuModelList = new ArrayList<SettleCardInfoModel>();
		for (SettleCardInfo o : list) {
			SettleCardInfoModel temp = new SettleCardInfoModel();
			BeanUtils.copyProperties(o, temp);
			menuModelList.add(temp);
		}
		int count = dao.countByExample(example);
		map.put("count", count);
		map.put("list", menuModelList);
		return map;
	}
	/**
	 * 分页查询
	 * 银行更换
	 * @param pageSize
	 * @param pageNum
	 * @param menuModel
	 * @return
	 * @throws DataAccessException
	 */
	public Map getChangePageByDomain(int pageSize, int pageNum, SettleCardInfoDomain domain)
			throws DataAccessException {

		Map<String, Object> map = new HashMap<String, Object>();
		//商户账户 借用于查询sysAccount
		String merchantName = domain.getMerchantName();
		//代理商编号 借用于查询sysAccount
		String companyName = domain.getCompanyName();

		SettleCardInfoExample example = new SettleCardInfoExample();
		//查出所有商户，再根据商户的id查
		com.zx.map.dao.model.SettleCardInfoExample.Criteria criteria = example.createCriteria();
		//查出所有商户，再根据商户的id查
		MerchantInfoExample merchantInfoExample = new MerchantInfoExample();
		if(StringUtils.isNotBlank(merchantName)){
			merchantInfoExample.createCriteria().andSysAccountLike('%'+merchantName+'%');
			List<MerchantInfo> merchantInfoList = merchantInfoDao.getListByExample(merchantInfoExample);
			List<String> merchantIdList = new ArrayList();
			if(merchantInfoList!=null&&merchantInfoList.size()>0){
				for(MerchantInfo merchantInfo : merchantInfoList){
					String merchantId = merchantInfo.getMerchantId();
					merchantIdList.add(merchantId);
				}
			}else{
				String merchantId = "-1";
				merchantIdList.add(merchantId);
			}
			
			com.zx.map.dao.model.SettleCardInfoExample.Criteria criteria1 = example.createCriteria();
			criteria1.andSubjectIdIn(merchantIdList);
			
			//or查询
			example.or(criteria1);
		}
		//查出所有代理商，再根据代理的id查
		AgentInfoExample agentInfoExample = new AgentInfoExample();
		if(StringUtils.isNotBlank(companyName)){
			agentInfoExample.createCriteria().andSysAccountLike('%'+companyName+'%');
			List<AgentInfo> agentInfoList = agentInfoDao.getListByExample(agentInfoExample);
			List<String> agentIdList = new ArrayList();
			if(agentInfoList!=null&&agentInfoList.size()>0){
				for(AgentInfo agentInfo : agentInfoList){
					String agentId = agentInfo.getAgentId();
					agentIdList.add(agentId);
				}
			}else{
				String agentId = "-1";
				agentIdList.add(agentId);
			}
			
			com.zx.map.dao.model.SettleCardInfoExample.Criteria criteria1 = example.createCriteria();
			criteria1.andSubjectIdIn(agentIdList);
			
			//or查询
			example.or(criteria1);
		}
		
		int pageBegin = (pageNum - 1) * pageSize;
		List<SettleCardInfo> list = dao.getPageByExample(pageSize, pageBegin, example);
		List<SettleCardInfoModel> modelList = new ArrayList<SettleCardInfoModel>();
		for (SettleCardInfo o : list) {
			SettleCardInfoModel temp = new SettleCardInfoModel();
			BeanUtils.copyProperties(o, temp);
			//查出对应的商户or代理商编号
			String subjectType = temp.getSubjectType();
			if("1".equals(subjectType)){//商户
				String merchantInfoId = temp.getSubjectId();
				MerchantInfo merchantInfo = merchantInfoDao.selectByPrimaryKey(merchantInfoId);
				if(merchantInfo!=null){
					temp.setSysAccount(merchantInfo.getSysAccount());
					temp.setMerchantName(merchantInfo.getMerchantName());
				}
			}else{//代理商
				String agentId = temp.getSubjectId();
				AgentInfo agentInfo = agentInfoDao.selectByPrimaryKey(agentId);
				if(agentInfo!=null){
					temp.setSysAccount(agentInfo.getSysAccount());
					temp.setCompanyName(agentInfo.getCompanyShortName());
				}
				
			}
			//查出当前对象是否在审核任务中
			Integer settleId = temp.getSettleId();
			TodoTaskModel todoTaskModel = new TodoTaskModel();
			todoTaskModel.setTaskKey(String.valueOf(settleId));
			todoTaskModel.setTaskType("6");
			todoTaskModel.setApproveStatus("0");
			List taskList = todoTaskService.getListByModel(todoTaskModel);
			if(taskList!=null&&taskList.size()>0){
				temp.setApplyStatus("0");//有待审核任务
			}else{
				temp.setApplyStatus("1");//无待审核任务
			}
			modelList.add(temp);
		}
		int count = dao.countByExample(example);
		map.put("count", count);
		map.put("list", modelList);
		return map;
	}
	/**
	 *根据代理商或商户信息查找
	 */
	public SettleCardInfoModel getModelByInfo(SettleCardInfoModel model)
			throws DataAccessException {
		SettleCardInfoExample example = new SettleCardInfoExample();
		com.zx.map.dao.model.SettleCardInfoExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(model.getSubjectId())) {
			criteria.andSubjectIdEqualTo(model.getSubjectId());
		}
		if (StringUtils.isNotBlank(model.getSubjectType())) {
			criteria.andSubjectTypeEqualTo(model.getSubjectType());
		}
		List<SettleCardInfo> list = dao.getListByExample(example);
		List<SettleCardInfoModel> modelList = new ArrayList<SettleCardInfoModel>();
		for (SettleCardInfo o : list) {
			SettleCardInfoModel temp = new SettleCardInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		if(modelList!=null&&modelList.size()>0){
			return modelList.get(0);
		}
		return null;
	}
}
